package cn.edu.nit.sys.mapper;

import cn.edu.nit.sys.entity.Menu;
import cn.edu.nit.sys.entity.RoleMenu;
import cn.edu.nit.sys.vo.MenuVO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author xiaobai
 * @Date 2022/8/24 22:03
 * @Version 1.0.0
 */
@Mapper
public interface MenuMapper extends BaseMapper<Menu> {

    List<Menu> getRoutes(String uid);

    Page<Menu> getMenuList(Page<Menu> page, String name);

    @Select("select * from sys_menu")
    List<Menu> getAll();

    List<Menu> getMenuById(List<String> menuIds);

    @Delete("DELETE a, b  from sys_menu a LEFT JOIN sys_role_menu b ON a.id = b.menu_id WHERE a.id = #{id}")
    Integer delMenu(Integer id);

    @Insert("insert into sys_menu(label, name, path, component, icon, parent, sort) values(" +
            "#{menu.label},#{menu.name},#{menu.path},#{menu.component},#{menu.icon},#{menu.parent},#{menu.sort})")
    @SelectKey(before = false, resultType = Integer.class, keyProperty = "menu.id", statement = "SELECT LAST_INSERT_ID()")
    Integer addMenu(@Param("menu") Menu menu);

    @Insert("insert into sys_role_menu(menu_id, role_id) values(#{menuId}, #{roleId})")
    Integer addRoleMenu(int menuId, Integer roleId);

    @Update("update sys_menu set label = #{menu.label}, name = #{menu.name}, path = #{menu.path}, " +
            "component = #{menu.component}, icon = #{menu.icon}, parent = #{menu.parent}, sort = #{menu.sort} where id = #{menu.id}")
    Integer updateMenu(@Param("menu") MenuVO menuVO);

    @Update("update sys_role_menu set role_id = #{roleId} where id = #{relationId}")
    Integer updateRoleMenu(Integer roleId, Integer relationId);

    @Select("select * from sys_role_menu where role_id = #{roleId} and menu_id = #{menuId}")
    RoleMenu selectRoleMenuByRoleAndMenu(Integer roleId, Integer menuId);

    @Update("update sys_menu set disabled = #{disabled} where id = #{id}")
    boolean lockMenu(Integer id, Integer disabled);

    @Select("select * from sys_role_menu where menu_id = #{id}")
    List<RoleMenu> selectAllRoleMenu(Integer id);

    Integer delRoleMenu(List<Integer> roleIds, Integer menuId);

    @Delete("delete from sys_role_menu where menu_id = #{menuId}")
    Integer delAllRoleMenuByMenuId(Integer menuId);
}
